import random
import os
from dbFileProcess import dbFileProcess
from strategyImpl.htmlStrategy import htmlStrategy
from strategyImpl.txtStragy import txtStrategy
class sentensDB:
    db_path = ''
    def __init__(self, path='../resource/db/') -> None:
        self.db_path = path
    def get_sentens(self, word):
        file_l = os.listdir(self.db_path)
        sentens_l = []
        for file_name in file_l:
            file_path = os.path.join(self.db_path, file_name)
            root, extent = os.path.splitext(file_path)
            if (extent == '.txt'):
                dbFileProc = dbFileProcess(path=file_path, strategy=txtStrategy())
            elif (extent == '.html'):
                dbFileProc = dbFileProcess(path=file_path, strategy=htmlStrategy())
            sentens_l.extend(dbFileProc.get_matched_sentens(word=word))
        return sentens_l[random.randint(0, len(sentens_l)-1)] if len(sentens_l)>0 else ''